package com.aaa.dao.impl;

import com.aaa.dao.BusinessDao;
import com.aaa.util.BaseDao;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author ：Teacher陈（86521760@qq.com）
 * @date ：Created in 2021/7/19 9:52
 * @description：活动dao实现类
 * @modified By：
 * @version: 1.0
 */
public class BusinessDaoImpl   extends BaseDao implements BusinessDao {
    /**
     * @create by: Teacher陈（86521760@qq.com）
     * @description: 查询所有的活动
     * @create time: 2021/7/19 9:55
     * @param map
     * @return java.util.List<java.util.Map<java.lang.String,java.lang.Object>>
     */
    @Override
    public List<Map<String, Object>> findAllBusiness(Map map) {

        String sql="select * from business_hospital";
        String makeSql = makeSql(sql, map);
        return super.executeQuery(makeSql,null);
    }

    @Override
    public int getCount(Map map) {
        StringBuffer stringBuffer = new StringBuffer(" select  count(*) myCount from business_hospital where 1=1 ");
        //拼接where条件  where username like  '%2%'
        Set set = map.keySet();
        for (Object o : set) {
            String  key=o.toString();
            if(key.contains("search")){
                //如果map的key中包含和关键字search，那么就是条件查询的条件
                stringBuffer.append(" and "+key.replace("search","")+" like "+"'%"+map.get(key)+"%'");
            }
        }
        List<Map<String, Object>> mapList = super.executeQuery(stringBuffer.toString(), null);
        if (mapList != null && mapList.size() > 0) {
            //获取第一行数据
            Map<String, Object> objectMap = mapList.get(0);
            //获取列名为myCount的列值
            Object count = objectMap.get("myCount");
            //将object类型的列值转换成int类型并返回
            return Integer.parseInt(count + "");
        }
        return 0;
    }
    /**
     * @create by: Teacher陈（86521760@qq.com）
     * @description: 查询报名统计
     * @create time: 2021/7/20 11:21
     * @param
     * @return java.util.List<java.util.Map<java.lang.String,java.lang.Object>>
     */
    @Override
    public List<Map<String, Object>> getApplyCount() {

        String sql="select  count(*) doneApply,s.business_id,bh.name,bh.count as planApply  from student_hospital s inner  join  business_hospital bh on s.business_id = bh.id group by s.business_id";
        return executeQuery(sql,null);
    }
    /**
     * @create by: Teacher陈（86521760@qq.com）
     * @description: 活动下线
     * @create time: 2021/7/23 9:54
     * @param map
     * @return boolean
     */
    @Override
    public boolean outlineBusinessById(Map map) {
        String  sql="update business_hospital set status =1 where id=?";
        Object[] params={map.get("id")};
        return executeUpdate(sql,params);
    }
}
